home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / What's New? / Sample Code / Sound / MP3Player / Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  9.5 KB  |  341 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Main.c
  3.     
  4.     Description: I'm just a main, yes I'm only a main.
  5.  
  6.     Author:        mc, era
  7.  
  8.     Copyright:     © Copyright 2000 Apple Computer, Inc. All rights reserved.
  9.     
  10.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  11.                 ("Apple") in consideration of your agreement to the following terms, and your
  12.                 use, installation, modification or redistribution of this Apple software
  13.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  14.                 please do not use, install, modify or redistribute this Apple software.
  15.  
  16.                 In consideration of your agreement to abide by the following terms, and subject
  17.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  18.                 copyrights in this original Apple software (the "Apple Software"), to use,
  19.                 reproduce, modify and redistribute the Apple Software, with or without
  20.                 modifications, in source and/or binary forms; provided that if you redistribute
  21.                 the Apple Software in its entirety and without modifications, you must retain
  22.                 this notice and the following text and disclaimers in all such redistributions of
  23.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  24.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  25.                 Apple Software without specific prior written permission from Apple.  Except as
  26.                 expressly stated in this notice, no other rights or licenses, express or implied,
  27.                 are granted by Apple herein, including but not limited to any patent rights that
  28.                 may be infringed by your derivative works or by other works in which the Apple
  29.                 Software may be incorporated.
  30.  
  31.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  32.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  33.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  35.                 COMBINATION WITH YOUR PRODUCTS.
  36.  
  37.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  38.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  41.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  42.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  43.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.                 
  45.     Change History (most recent first): <2> 7/26/00 carbonized
  46.                                         <1> 4/01/00 initial release
  47. */
  48.  
  49. #include "MP3Player.h"
  50.  
  51. // globals
  52. Boolean gDone = false;
  53. BitMap    gScreenbits;
  54.  
  55. static pascal OSErr HandleOApp(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long handlerRefcon);
  56. static pascal OSErr HandleOApp(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long handlerRefcon)
  57. {
  58. #pragma unused(theAppleEvent, reply, handlerRefcon)
  59.  
  60.     return noErr;    // We're up and running
  61. }
  62.  
  63. static pascal OSErr HandlePDoc(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long handlerRefcon);
  64. static pascal OSErr HandlePDoc(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long handlerRefcon)
  65. {
  66. #pragma unused(theAppleEvent, reply, handlerRefcon)
  67.  
  68.     return noErr;
  69. }
  70.  
  71. static pascal OSErr HandleQuit(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long handlerRefcon);
  72. static pascal OSErr HandleQuit(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long handlerRefcon)
  73. {
  74. #pragma unused(theAppleEvent, reply, handlerRefcon)
  75.  
  76.     gDone = true;
  77.     return noErr;
  78. }
  79.  
  80. static pascal OSErr HandleODoc(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long handlerRefcon);
  81. static pascal OSErr HandleODoc(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long handlerRefcon)
  82. {
  83. #pragma unused(reply, handlerRefcon)
  84.  
  85.     AEDescList    docList;
  86.     long        index = 1, itemsInList;
  87.     Size        actualSize;
  88.     AEKeyword    keywd;
  89.     DescType    returnedType;
  90.     
  91.     OSErr        err;
  92.  
  93.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList);
  94.     if (err == noErr) {
  95.         err = AECountItems(&docList, &itemsInList);
  96.     }
  97.  
  98.     if (err == noErr) {
  99.         FSSpecPtr fileSpecPtr;
  100.  
  101.         do {
  102.             fileSpecPtr = (FSSpecPtr)NewPtr(sizeof(FSSpec));
  103.             err = MemError ();
  104.  
  105.             if (err == noErr) {
  106.                 err = AEGetNthPtr(&docList, index, typeFSS, &keywd, &returnedType, fileSpecPtr, sizeof(FSSpec), &actualSize);
  107.             }
  108.  
  109.             if (err == noErr) {
  110.                 HParamBlockRec pb;
  111.  
  112.                 pb.fileParam.ioCompletion = NULL;
  113.                 pb.fileParam.ioNamePtr = fileSpecPtr->name;
  114.                 pb.fileParam.ioVRefNum = fileSpecPtr->vRefNum;
  115.                 pb.fileParam.ioDirID = fileSpecPtr->parID;
  116.                 pb.fileParam.ioFDirIndex = 0;
  117.  
  118.                 err = PBHGetFInfoSync(&pb);
  119.                 if (err == noErr && pb.fileParam.ioFlFndrInfo.fdType != kPreferencesFolderType) {
  120.                     err = PlaySound(fileSpecPtr);    // get to the meat of the issue, play the damn file already!
  121.                     DisposePtr((Ptr)fileSpecPtr);
  122.                 }
  123.             }
  124.  
  125.             index++;
  126.         } while (err == noErr);
  127.  
  128.         // The last time through the loop we allocate a pointer we don't need.
  129.         DisposePtr((Ptr)fileSpecPtr);
  130.     }
  131.  
  132.     AEDisposeDesc(&docList);
  133.  
  134.     return err;
  135. }
  136.  
  137. static OSErr GetSoundToPlay(FSSpec *fileToPlay);
  138. static OSErr GetSoundToPlay(FSSpec *fileToPlay)
  139. {    
  140.     OSErr err = noErr;
  141.  
  142. #ifndef TARGET_API_MAC_CARBON
  143.     if (NavServicesAvailable() == true) {
  144. #else
  145. #pragma unused(fileToPlay)
  146. #endif
  147.         NavReplyRecord        navReply;
  148.         NavDialogOptions    dialogOptions;
  149.  
  150.         err = NavGetDefaultDialogOptions(&dialogOptions);
  151.         if (err == noErr) {
  152.             dialogOptions.dialogOptionFlags = kNavAllFilesInPopup;
  153.         }
  154.  
  155.         if (err == noErr) {
  156.             err = NavGetFile(NULL, &navReply, &dialogOptions, NULL, NULL, NULL, NULL, NULL);
  157.         }
  158.  
  159.         if (navReply.validRecord && err == noErr) {
  160.             ProcessSerialNumber processSN = {0, kCurrentProcess};
  161.             AEAddressDesc        targetAddress = {typeNull, NULL};
  162.             AppleEvent            theODOC = {typeNull, NULL},
  163.                                 theReply = {typeNull, NULL};
  164.  
  165.             // Create an Apple Event to ourselves.
  166.             err = AECreateDesc(typeProcessSerialNumber, &processSN, sizeof(ProcessSerialNumber), &targetAddress);
  167.  
  168.             if (err == noErr) {
  169.                 // Create the open document event.
  170.                 err = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &targetAddress, kAutoGenerateReturnID, kAnyTransactionID, &theODOC);
  171.                 AEDisposeDesc(&targetAddress);
  172.             }
  173.  
  174.             if (err == noErr) {
  175.                 // Put the list of files into the open document event Apple Event.
  176.                 err = AEPutParamDesc (&theODOC, keyDirectObject, &(navReply.selection));
  177.             }
  178.  
  179.             if (err == noErr) {
  180.                 // Send the open document event to ourselves.
  181.                 err = AESend(&theODOC, &theReply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
  182.                 AEDisposeDesc(&theODOC);
  183.                 AEDisposeDesc(&theReply);
  184.             }
  185.  
  186.         }
  187.         
  188.         NavDisposeReply (&navReply);
  189.  
  190. #ifndef TARGET_API_MAC_CARBON        
  191.     } else {
  192.         SFTypeList            typeList = {'AIFF', 'AIFC', 0, 0};
  193.         StandardFileReply    sfReply;
  194.         
  195.         StandardGetFile(nil, 2, typeList, &sfReply);
  196.  
  197.         if (sfReply.sfGood == true) {
  198.             *fileToPlay = sfReply.sfFile;
  199.             err = PlaySound(fileToPlay);
  200.         } else {
  201.             err = userCanceledErr;
  202.         }
  203.     }
  204. #endif
  205.  
  206.     return err;
  207. }
  208.  
  209. static OSErr DispatchMenuChoice(long menuChoice);
  210. static OSErr DispatchMenuChoice(long menuChoice)
  211. {
  212.     short        menu;
  213.     short        item;
  214.     FSSpec        fileToPlay;
  215.     
  216.     OSErr        err = noErr;
  217.  
  218.     if (menuChoice != 0) {
  219.         menu = HiWord(menuChoice);
  220.         item = LoWord(menuChoice);
  221.         switch (menu) {
  222.         case kFileMenu:
  223.             switch (item) {
  224.                 case kOpenItem:
  225.                     err = GetSoundToPlay(&fileToPlay);
  226.                     break;
  227.                 case kQuitItem:
  228.                     gDone = true;
  229.                     break;
  230.             }
  231.         }
  232.     }
  233.  
  234.     HiliteMenu (0);
  235.  
  236.     return err;
  237. }
  238.  
  239. static OSErr InstallRequiredAppleEvents(void);
  240. static OSErr InstallRequiredAppleEvents(void)
  241. {
  242.     OSErr err;
  243.  
  244.     err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerUPP(HandleOApp), 0, false);
  245.  
  246.     if (err == noErr)
  247.         err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerUPP(HandleODoc), 0, false);
  248.  
  249.     if (err == noErr)
  250.         err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerUPP(HandlePDoc), 0, false);
  251.  
  252.     if (err == noErr)
  253.         err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP(HandleQuit), 0, false);
  254.  
  255.     return err;
  256. }
  257.  
  258. static OSErr MenuBarInit (void);
  259. static OSErr MenuBarInit (void)
  260. {
  261.     Handle        menuBar;
  262.     MenuHandle    menu;
  263.     
  264.     OSErr        err = noErr;
  265.  
  266.     menuBar = GetNewMBar(128);
  267.     if (menuBar != nil) {
  268.         SetMenuBar (menuBar);
  269.         menu = GetMenuHandle(128);
  270.         if (menu != nil) {
  271.             DrawMenuBar ();
  272.         } else {
  273.             err = memFullErr;
  274.         }
  275.     } else {
  276.         err = memFullErr;
  277.     }
  278.  
  279.     return err;
  280. }
  281.  
  282. // --------------------
  283. // Initialize for Carbon & QuickTime
  284. //
  285. static OSErr Initialize(void);
  286. static OSErr Initialize(void)
  287. {
  288.     OSErr err = noErr;
  289.     
  290.     InitCursor();
  291.     EnterMovies();
  292.     
  293.     GetQDGlobalsScreenBits(&gScreenbits);
  294.     
  295.     err = InstallRequiredAppleEvents();
  296.     err = MenuBarInit();
  297.     
  298.     return err;
  299. }
  300.  
  301. void main(void) {
  302.     Boolean        gotEvent;
  303.     EventRecord theEvent;
  304.     WindowRef    theWindow;
  305.     short        thePart;
  306.     
  307.     OSErr        err = noErr;
  308.     
  309.     err = Initialize();
  310.  
  311.     while (!gDone) {
  312.         gotEvent = WaitNextEvent(everyEvent, &theEvent, 10, NULL);
  313.  
  314.         if (gotEvent) {
  315.             switch (theEvent.what) {
  316.             case kHighLevelEvent:
  317.                 err = AEProcessAppleEvent(&theEvent);
  318.                 break;
  319.                 
  320.             case mouseDown:
  321.                 thePart = FindWindow (theEvent.where, &theWindow);
  322.                 switch (thePart) {
  323.                     case inMenuBar:
  324.                         DispatchMenuChoice(MenuSelect(theEvent.where));
  325.                         break;
  326.                 }
  327.                 break;
  328.                 
  329.             case keyDown:
  330.                 if (theEvent.modifiers & cmdKey) {
  331.                     err = DispatchMenuChoice(MenuKey(theEvent.message & charCodeMask));
  332.                 }
  333.                 break;
  334.             
  335.             default:
  336.                 break;
  337.                 
  338.             } // switch
  339.         }
  340.     }
  341. }